/**
 * 交易流水管理初始化
 */
var MchReg = {
    id: "MchRegTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
MchReg.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: '商户编号', field: 'merCode',width:"150px", visible: true, align: 'center', valign: 'middle'},
            {title: '商户名称', field: 'merName', visible: true, align: 'center', valign: 'middle'},
            {title: '真实姓名', field: 'realName', visible: true, align: 'center', valign: 'middle'},
            {title: '手机号', field: 'hideMobile',width:"120px", visible: true, align: 'center', valign: 'middle'},
            {title: '结算账号',field: 'hideAccountNo',width:"180px", visible: true, align: 'center', valign: 'middle'},
            {title: '银行名称', field: 'bankName', visible: true, align: 'center', valign: 'middle'},
            {title: '银行编号', field: 'bankCode', visible: true, align: 'center', valign: 'middle'},
            {title: '联行号', field: 'subBankCode', width:"130px",visible: true, align: 'center', valign: 'middle'},
            {title: '状态', field: 'status', visible: true, align: 'center', valign: 'middle',formatter:"statusFormatter"},
            {title: '黑名单', field: 'isBlack', visible: true, align: 'center', valign: 'middle',formatter:"isBlackFormatter"},
            {title: '创建时间', field: 'createTime', width:"150px", visible: true, align: 'center', valign: 'middle'},
            {field: 'action', title: '操作', align: 'center', formatter: 'actionFormatter', events: 'actionEvents', clickToSelect: false}
    ];
};



//格式化操作按钮
function actionFormatter(value, row, index) {
 return [
     '<a class="mchBus" href="javascript:;" onclick="MchReg.mchBusAction()" data-toggle="tooltip" title="业务开通"><i class="glyphicon glyphicon-search"></i></a>　'
 ].join('');
}

/**
 * 状态格式化
 */
function statusFormatter(value, row, index){
	if (value == 0) {
		return '<span class="label label-default">未注册</span>';
	} else if(value ==1) {
		return '<span class="label label-success">成功</span>';
	}else if(value ==2){
		return '<span class="label label-danger">失败</span>';
	}
}

function isBlackFormatter(value, row, index){
	if (value == 0) {
		return '<span class="label label-success">正常</span>';
	} else if(value ==1) {
		return '<span class="label label-default">拉黑</span>';
	}
}

/**
 * 检查是否选中
 */
MchReg.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        MchReg.seItem = selected[0];
        return true;
    }
};

/**
 * 开通业务
 */
MchReg.mchBusAction = function(){   
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '查看开通业务',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/mchReg/mchReg_bus/' + MchReg.seItem.id
        });
        this.layerIndex = index;
    }
	
}

/**
 * 点击添加交易流水
 */
MchReg.openAddMchReg = function () {
    var index = layer.open({
        type: 2,
        title: '添加交易流水',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/mchReg/mchReg_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看交易流水详情
 */
MchReg.detail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '交易流水详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/mchReg/mchReg_detail/' + MchReg.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 导出
 */
MchReg.download = function () {
	var merCode= $("#merCode").val();
    var realName = $("#realName").val();
    var beginTime = $("#beginTime").val();
    var endTime = $("#endTime").val(); 
    var status = $("#status").val(); 
	window.location.href=Feng.ctxPath + "/mchReg/download?merCode"+merCode+"&realName="+realName+"&beginTime="+beginTime+"&endTime="+endTime+"&status="+status;
};



/**
 * 加入黑名单
 */
MchReg.addBlack = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/mchReg/black", function (data) {
            Feng.success("成功!");
            MchReg.table.refresh();
        }, function (data) {
            Feng.error("失败!" + data.responseJSON.message + "!");
        });
        ajax.set("mchRegId",this.seItem.id);
        ajax.start();
    }
};


/**
 * 删除交易流水
 */
MchReg.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/mchReg/delete", function (data) {
            Feng.success("删除成功!");
            MchReg.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("mchRegId",this.seItem.id);
        ajax.start();
    }
};

MchReg.formParams = function() {
    var queryData = {};
    queryData['merCode'] = $("#merCode").val();
    queryData['realName'] = $("#realName").val();
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val(); 
    queryData['status'] = $("#status").val(); 

    return queryData;
}

/**
 * 查询交易流水列表
 */
MchReg.search = function () {
	
	MchReg.table.refresh({query: MchReg.formParams()});
};



$(function () {
    var defaultColunms = MchReg.initColumn();
    var table = new BSTable(MchReg.id, "/mchReg/list", defaultColunms);
    table.setPaginationType("server");//服务器端分页
    table.setQueryParams(MchReg.formParams());
    MchReg.table = table.init();
});
